<?php
class SF_CategoryPage_Block_Automobiles extends Mage_Catalog_Block_Product_Abstract
{
	protected function _toHtml() {
        return parent::_toHtml();
    }
    public function getProuducts(){
        $_products['sizes']= 0;
        $categorys = $this->getProductSkus();
        $_collectionSize = count($categorys);
        if($_collectionSize > 5){
            $_products['info']='success';
        }
        $categorys = array_slice($categorys,0,5);
        $_products['products'] = $this->getCategoru($categorys);
        return $_products;
    }
    public function getProuductsClick($size){
        $sizes = ($size-1)*5;
        $categorys = $this->getProductSkus();
        $_collectionSize = count($categorys);
        $_products['sizes']=$sizes;
        if($_collectionSize > $sizes){
            $categorys = array_slice($categorys,$sizes,5);
            $sizelist = count($categorys);
            if($sizelist < '5'){
                $_products['info']='cease';
            }else{
                $_products['info']='success';
            }
            $_products['products'] = $this->getCategoru($categorys);
        }else{
            $_products['info']='ends';
        }
        return $_products;
    }

    public function getCategoru($categorys){
        $this_store  = Mage::app()->getStore()->getStoreId();//视图判断
        $products='';
        $collect = array();
        foreach($categorys as $key => $category){
            $categoryName = ($this_store == '2')? $category['categoryName']['1']:$category['categoryName']['0'];
            $productsLi =$this->getCategory($category['products']);
            //if(!empty($productsLi)){
                $products .='<div class="sf-products-list"><div class="em-line-01 button-show01 button-hide-text" style="position: relative;">';
                $products .='<div class="widget-title em-widget-title"><h3 class="categoryBorder categoryColor"><span>'.$categoryName.'</span></h3></div></div>';
                $products .='<div class="em-featured-slider"><ul class="emcatalog-grid-mode products-grid emcatalog-disable-hover-below-mobile">'.$productsLi.'</ul></div></div>';
                $productss[] =$this->getCategory($category['products']);
            //}
        }
        return $products;
    }

    public function getCategory($productSkus)
    {
        $collect = array();
        $products = $this->getCatProducts($productSkus);
        $collections = $this->getCollection($products);
        return $collections;
    }

    public function getCatProducts($productSkus){
        $products = array();
        $product_s = Mage::getResourceModel('catalog/product_collection')
                        // ->addAttributeToSelect('*')
                        ->addAttributeToSelect('id')
                        ->addAttributeToSelect('name')
                        ->addUrlRewrite()
                        ->addAttributeToSelect('short_description')
                        ->addAttributeToSelect('thumbnail')
                        ->addAttributeToSelect('pirce')
                        ->addFinalPrice();
        $_products = $product_s->addAttributeToFilter('SKU', array('in' => $productSkus));
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_products); 
        foreach($_products as $product)
        { 
        
            // if($product->getTypeId() == 'simple')
            // {
            //     $qtyStock = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
            // }
            // if($product->getTypeId() == 'configurable')
            // {
            //     $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($product);
            //     $col = $conf->getUsedProductCollection()->addAttributeToSelect('status')->addFilterByRequiredOptions();
            //     $qtyStock = 0;
            //     foreach ($col as $sprod){
            //         if($sprod->getStatus() == '2') continue;
            //         $qty = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($sprod)->getQty());
            //         $qtyStock += $qty;
            //     }
            // }
            $productid = $product->getId();
            $products[$key][] = array(
                    'id'             => $product->getId(),
                    'sku'            => $product->getSku(),
                    'name'           => $product->getName(),
                    'url'            => $product->getProductUrl(),
                    'thumbnail'      => (string)Mage::helper('catalog/image')->init($product, 'thumbnail')->resize('190'),
                    'short_description' => $product->getShortDescription(),
                    //'wishlist'       => Mage::helper('wishlist')->getAddUrl($product),
                    'price'          => $product->getPrice(),
                    'currency_price' => Mage::helper('core')->currency($product->getPrice(), true, false),
                    'final_price'    => $product->getFinalPrice(),
                    'currency_final_price' => Mage::helper('core')->currency($product->getFinalPrice(), true, false),
                    //'qty'            => $qtyStock,
                    'available'       => $product->isAvailable(),
            );
            $productids[] = $product->getId();
        }
        
        return $products;
    }

	public function getCollection($collection){
        $product = '';
        $productImg = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product/placeholder/default/placeholder-image-rumall.jpg';
        $i = '0';
        $w = '190';
        $h = '190';
        $_collectionSize = count($collection)."</br/>";
        if($_collectionSize > 0){
            foreach ($collection as $products) {
                foreach ($products as $_products) {
                    $shortdes = $_products['shortdes'];
                    if(mb_strlen($shortdes, 'utf-8')>100) {
                        $shortdes = rtrim(mb_substr($shortdes, 0, 100, 'utf-8')) . '...';
                    }
                    //$clas01 = $i == 0 ?' first':'''.$clas01.$clas02.';
                    //$clas02 = $i+1 == $_collectionSize ?' last':'';
                    $product .= '<li class="item col-sm-4"><div class="product-item">';

                    $product .= '<div class="product-shop-top">';
                    $product .= '<a class="" href="'.$_products['url'].'" title="'.$_products['name'].'">';
                    //$product .= '<img class="img-responsive em-alt-hover lazy" src="'.$_products['thumbnail'].'" alt=""/>';
                    $product .= '<img class="img-responsive em-alt-org lazy" src="'.$_products['thumbnail'].'" alt="" id="product-collection-image-'.$_products['id'].'"/>';
                    $product .= '<span class="bkg-hover"></span></a>';

                    // $product .= '<div class="bottom">';
                    // if($_product->isSaleable()){
                    //     $product .= '<button type="button" title="'. Mage::helper("core")->quoteEscape($this->__("Add to Cart")) .'" class="button btn-cart" onclick=setLocation("'. $this->getAddToCartUrl($_product) .'")><span><span>'. $this->__("Add to Cart") .'</span></span></button>';
                    // }else{
                    //     $product .= '<p class="availability out-of-stock"><span>'. $this->__("Out of stock") .'</span></p>';
                    // }
                    // $product .= '<ul class="add-to-links"><li><a href="'. $this->helper("wishlist")->getAddUrl($_product) .'" class="link-wishlist" title="'. $this->__("Add to Wishlist") .'">'. $this->__("Add to Wishlist") .'</a></li><li><a href="'. $this->getAddToCompareUrl($_product).'" class="link-compare" title="'. $this->__("Add to Compare") .'">'. $this->__("Add to Compare") .'</a></li></ul>';
                    // $product .= '</div>';
                    $product .= '</div>';

                     $product .= '<div class="product-shop"><div class="f-fix">';
                    $product .= '<h2 class="product-name text-center"><a href="'.$_products['url'].'" title="'.$_products['name'].'">'.$_products['name'].'</a></h2>';
                    //$product .= '<div class="text-center">'.$_products['reviews'].'</div>';
                    if($_products['price'] == $_products['final_price']){
                        $product .= '<div class="text-center"><div class="price-box"><span class="regular-price" id="product-price-8170"><span class="price">'.$_products['currency_price'].'</span></span></div></div>';
                    }else{
                        $product .= '<div class="text-center"><div class="price-box"><p class="old-price"><span class="price-label">Regular Price:</span><span class="price" id="old-price-'.$_products['id'].'">'.$_products['currency_price'].'</span></p><p class="special-price"><span class="price-label">Special Price</span><span class="price" content="9" id="product-price-'.$_products['id'].'">'.$_products['currency_final_price'].'</span></p></div></div>';

                    }
                    $product .= '</div></div>';
                    
                    if(!$_products['available']){
                        $product .= '<div class="page_instock"></div>';
                    }
                    //$product .= '<div class="desc std text-center em-element-display-hover">'.$shortdes.'</div>';
                    $product .= '</div></li>';
                    
                }

            }
            return $product;
        }else{
            return false;
        }
    }


    public function getProductSkus(){
        $collection = array(
            array(
                'categoryName'=>array('Smartphone','Смартфоны'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    '755S00010_HT000017',
                    '755S00084_A806',
                    '755S00084_K30W',
                    '755S00084_Z2131',
                    '755S00084_Z1221',
                    '755S00084_S60W',
                ),
            ),
            array(
                'categoryName'=>array('Wireless & Portable devices','Беспроводные и портативные устройства'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "755S00072_Xiaomi-PowerBank-20000mAh",
                    "755S00072_Xiaomi-ROIDMI-2S",
                    "755S00072_XiaoMi-Router-3",
                    "755S00072_Xiaomi-TDS",
                    /*"852S00001_ZUHC220-B10",*/
                    "852S00001_ZUHC217-B10",
                    "755S00072_Xiaomi-Selfie-Stick",
                ),
            ),
            array(
                'categoryName'=>array('Smart Home','Умный дом'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "755S00072_Xiaomi-yi-IP-Camera",
                    "755S00049_obqoA1",
                    "755S00072_Mibox-3",
                    "755S00072_Xiaomi-Humidity-Senso",
                    "755S00204_s0006",
                    "755S00024_ML002W",
                ),
            ),
            array(
                'categoryName'=>array('Fitness Tracker','Фитнес-трекеры'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "755S00017_YXF11958",
                    "755S00072_xiaomi-band-1S-Wristband",
                    "755S00072_miband-2",
                    "755S00017_YXF12001",
                    "755S00072_Xiaomi-Amazfit",
                    "755S00157_0800229",
                ),
            ),
            array(
                'categoryName'=>array('Smart Watches','Смарт-часы'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "755S00081_LF07",
                    "755S00081_E07S",
                    "755S00017_YXF14823",
                    "020S00026_u8001",
                    "755S00017_YXF13271",
                    "755S00017_YXF11875",
                ),
            ),
            array(
                'categoryName'=>array('Portable Camera','Портативные камеры'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "755S00072_xiaomi -yi",
                    "755S00072_xiaomi-yi-4K",
                    "755S00024_DVR63H",
                    "755S00024_DVR52",
                    "755S00024_DVR56",
                    "769S00004_HG-A5",
                ),
            ),
            array(
                'categoryName'=>array('Portable Speakers','Портативная акустика'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "755S00072_xiaomi-Hybrid",
                    "755S00072_Xiaomi_Speaker",
                    "755S00059_Joway-BM020",
                    /*"755S00024_YX001B",*/
                    "755S00072_Xiaomi-Bluetooth-Speaker ",
                    /*"755S00024_YX001B",*/
                    "755S00022_BS0114",
                    "755S00008_GT-01",
                ),
            ),
            array(
                'categoryName'=>array('VR Glasses','VR очки'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "755S00059_Joway-VR02",
                    "755S00059_Joway-VR03",
                    "755S00401_SH1-BTS",
                    "755S00072_Xiaomi-VR",
                    "020S00018_PIPOV1",
                    "755S00040_VR0001",
                ),
            ),
            array(
                'categoryName'=>array('Smart Lighting','Смарт-освещение'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "755S00072_Xiaomi-Yeelight-E27",  
                    "769S00006_xyd014",
                    "769S00006_sd002",
                    "755S00072_Xiaomi-Yeelight",
                    "755S00072_Xiaomi-Colorful-Yeelight",
                    /*"020S00026_RWXLED0001",*/
                    "755S00072_Xiaomi-Mijia-LED",
                ),
            ),
            array(
                'categoryName'=>array('Drones & Quadrocopters','Дроны и квадрокотеры'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "755S00022_GT-MJX001-O",
                    "755S00022_GT-MJX004-U-2",
                    "755S00022_GT-JZ002-U-3",
                    "755S00022_GT-MJX006-U-2",
                    "755S00022_GT-MJX003-U-1",
                    "755S00022_GT-MJX004-U-1",
                ),
            ),
            array(
                'categoryName'=>array('Beauty & Health','Красота и здоровье'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "755S00024_EEBT04",
                    "755S00024_RCS85RQ",
                    "755S00024_TS001W",
                    "574S00004_08020A019",
                    "755S00024_HS46WQ",
                    "755S00024_HS45RQ",
                ),
            ),
            array(
                'categoryName'=>array('Home & Garden','Дом и сад'),
                'categoryUrl'=>'http://127.0.0.1/magento_test/accessories.html',
                'products'=>array(
                    "574S00001_ET1002",
                    "574S00005_YL24015",
                    /*"755S00025_H17031",*/
                    "574S00005_YL21055",
                    "574S00005_YL21049",
                    "574S00005_YL21202",
                    "574S00005_YL21032",
                ),
            ),
        );
        return $collection;
    }
}